package com.charging.controller.admin;

import com.charging.entity.User;
import com.charging.service.ChargingPileService;
import com.charging.service.FaultService;
import com.charging.service.RecordService;
import com.charging.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private UserService userService;

    @Autowired
    private ChargingPileService chargingPileService;

    @Autowired
    private FaultService faultService;

    @Autowired
    private RecordService recordService;


    // 管理员仪表盘
    @GetMapping("/dashboard")
    public String dashboard(Model model, HttpSession session) {

        // 检查登录状态
        User user = (User) session.getAttribute("admin");
        // 检查用户是否登录
        Long adminid = user.getId();
        System.out.println("user"+user);
        if (adminid == null) {
            return "redirect:/user/login";
        }
        // 获取统计数据
        // 1. 获取用户总数（假设通过服务层查询）
        long userCount = userService.getTotalUserCount();

        // 2. 将 userCount 添加到模型（Model）中，供 Thymeleaf 使用
        model.addAttribute("userCount", userCount);

        // 1. 获取充电桩总数（假设通过服务层查询）
        long pileCount = chargingPileService.getTotalPileCount();

        // 2. 将 pileCount 添加到模型（Model）中，供 Thymeleaf 使用
        model.addAttribute("pileCount", pileCount);

        long pendingFaults = faultService.findFaultCount();
        model.addAttribute("pendingFaults", pendingFaults);

        long todayChargingKWh = recordService.findRecordEnergyCount();
        model.addAttribute("todayChargingKWh", todayChargingKWh);

        return "admin/dashboard";
    }

    @GetMapping("/settings")
    public String settings(Model model) {
        // 获取统计数据

        return "admin/settings";
    }
}